package com.aliyun.messagemgt.repository.mappers;

import com.aliyun.messagemgt.dto.MsgAdd;
import com.aliyun.messagemgt.dto.MsgSourceDto;
import com.aliyun.messagemgt.dto.query.BaseMessageSourceQuery;
import com.aliyun.messagemgt.dto.query.MsgQuery;
import com.aliyun.messagemgt.dto.vo.MsgSourceVO;
import com.aliyun.messagemgt.entities.BaseMessageSourceEntity;
import com.aliyun.messagemgt.entities.MsgSourceEntity;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author changcong
 */
public interface MsgSourceMapper {

    int countMsgSouece(MsgQuery msgQuery);

    /**
     * 注册发布消息源
     *
     * @param msgSource
     * @return
     */
    Integer addMsgSource(MsgSourceEntity msgSource);

    /**
     * 更新发布消息源
     *
     * @param msgSource
     * @return
     */
    Integer modMsgSource(MsgSourceEntity msgSource);

    /**
     * 查询消息源列表
     *
     * @param msgQuery
     * @return
     */
    Page<MsgSourceVO> list(MsgQuery msgQuery);

    /**
     * 查询消息源详细信息
     *
     * @param msgQuery
     * @return
     */
    MsgSourceVO queryMsgSourceDetail(MsgQuery msgQuery);

    /**
     * 查询分发消息列表
     *
     * @param msgQuery
     * @return
     */
   List<MsgSourceVO> queryMsgList(MsgQuery msgQuery);

    /**
     * 根据消息源名字查询消息源个数
     *
     * @param sourceName
     * @return
     */
    int selectCountByName(String sourceName);

    /**
     * 根据消息源名字查询消息源个数除了自身
     *
     * @param msgAdd
     * @return
     */
    int selectCountByNameExceptSelf(MsgSourceEntity msgAdd);

    /**
     * 删除消息源
     *
     * @param msgSourceId
     * @return
     */
    int delMsgSource(Long msgSourceId);

    /**
     * 查询渠道状态
     *
     * @param pushChannelId
     * @return
     */
    int queryChannelState(@Param("pushChannelId") Long pushChannelId);

    /**
     * 根据渠道名称查询一个消息源ID
     *
     * @param description
     * @return
     */
    String selectMsgSourceByChannelName(String description);

    /**
     * 查询没有初始化短信配额的短信消息源
     *
     * @return list
     */
    List<MsgSourceEntity> queryUnboundSmsQuotaMsgSource();

    /**
     * 通过发送任务code查询发送任务配置
     *
     * @param sourceCode UUID
     * @return 消息源
     */
    MsgSourceEntity queryBySourceCode(@Param("sourceId") String sourceCode);

    MsgSourceEntity getMsgSourceBySourceCode(String sourceCode);

    @Update("update msg_source set audit_status = ${auditStatus} where msg_source_id = ${msgSourceId}")
    Integer updateMsgSourceAuditStatus(@Param("msgSourceId") Integer msgSourceId, @Param("auditStatus") Integer auditStatus);


    @Select("select ms.msg_source_id msgSourceId, ms.source_name sourceName,ms.owner owner,ms.audit_status auditStatus from msg_source ms where ms.state_delete = 0 and (ms.push_channel_id = #{pushChannelId} or ms.resend_channel_id = #{pushChannelId})")
    List<MsgSourceEntity> selectMsgSourceByPushChannelId(@Param("pushChannelId") Integer pushChannelId);

    /**
     * 启停用
     * @param msgSourceDto
     * @return
     */
    Integer updateStatus(MsgSourceDto msgSourceDto);

    MsgSourceEntity getMsgSource(@Param("qry")MsgQuery qry);

    Integer selectMsgSourceIdByMessageCode(String messagecode);

    List<BaseMessageSourceEntity>  noSpecifiedBaseMessageSourcelist(@Param("baseMessageSourceQuery") BaseMessageSourceQuery baseMessageSourceQuery);

    /**
     * 新增查询
     * @param add
     * @return
     */
    Integer queryByCode(MsgAdd add);
}
